﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using PostSharp.Aspects;
using LuctecWS.Envelope;

namespace LuctecWS.Aspect
{
    [Serializable]
    public class ServiceBoundaryAttribute : OnMethodBoundaryAspect
    {
        public override void OnEntry(MethodExecutionArgs args)
        {
            BaseRQ req = args.Arguments.First() as BaseRQ;

            req.Start = DateTime.Now;
        }

        public override void OnException(MethodExecutionArgs args)
        {
        }

        public override void OnExit(MethodExecutionArgs args)
        {
            BaseRQ req = args.Arguments.First() as BaseRQ;
            BaseRS res = args.ReturnValue as BaseRS;

            res.End = DateTime.Now;

            res.ExecuteServerTime = res.End.Subtract(req.Start).Milliseconds;
        }

        public override void OnSuccess(MethodExecutionArgs args)
        {
            BaseRS res = args.ReturnValue as BaseRS;

            res.Successful = res.Errors == null || res.Errors.Count == 0;
        }
    }
}